package com.example.yeujian5.yuejian5.model;

import com.example.yeujian5.yuejian5.Interface.TrainFace;
import com.example.yeujian5.yuejian5.Listenner.OrderListener;
import com.example.yeujian5.yuejian5.Listenner.TrainDetailListener;
import com.example.yeujian5.yuejian5.Listenner.TrainListener;
import com.example.yeujian5.yuejian5.bean.TrainBean;
import com.example.yeujian5.yuejian5.service.TrainService;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by wangmengmeng on 2017/10/9.
 */

public class TrainModel implements TrainFace {

    private Retrofit retrofit;
    private TrainService trainService;
    private static final String BASE_URL = "http://172.24.10.175/workout/api.php/";//主机地址

    public TrainModel() {
        retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    /**
     * 获得培训列表
     * @param listListener 监听对象
     */
    @Override
    public void getList(String id, String name,final TrainListener listListener) {
        trainService = retrofit.create(TrainService.class);
        Call<List<TrainBean>> call =trainService.getTrainList();
        call.enqueue(new Callback<List<TrainBean>>() {
            @Override
            public void onResponse(Call<List<TrainBean>> call, Response<List<TrainBean>> response) {
                if (response.isSuccessful() && response.body() != null) {
                    listListener.onResponse(response.body());
                } else {
                    listListener.onFail("onresponse fail");
                }
            }

            @Override
            public void onFailure(Call<List<TrainBean>> call, Throwable t) {
                listListener.onFail(t.toString());
            }
        });
    }

    //分级列表
    @Override
    public void getTrainClass(String SessionID, String degree,final TrainListener listListener) {
        trainService = retrofit.create(TrainService.class);
        Call<List<TrainBean>> call =trainService.getTrainClass("lgj98rfkiqvenhrk2o7oe2po81","train",degree);
        call.enqueue(new Callback<List<TrainBean>>() {
            @Override
            public void onResponse(Call<List<TrainBean>> call, Response<List<TrainBean>> response) {
                if (response.isSuccessful() && response.body() != null) {
                    listListener.onResponse(response.body());
                } else {
                    listListener.onFail("onresponse fail");
                }
            }

            @Override
            public void onFailure(Call<List<TrainBean>> call, Throwable t) {
                listListener.onFail(t.toString());
            }
        });
    }



    /**
     * 获得培训详情
     */


    @Override
    public void getTrainById(String sessionId, String id,final TrainDetailListener listener) {
        trainService = retrofit.create(TrainService.class);
        Call<TrainBean> call= trainService.getTrainById(sessionId,id);
        call.enqueue(new Callback<TrainBean>() {
            @Override
            public void onResponse(Call<TrainBean> call, Response<TrainBean> response) {
                if (response.isSuccessful() && response.body() != null) {
                    listener.onResponse(response.body());
                } else {
                    listener.onFail("onresponse fail");
                }
            }

            @Override
            public void onFailure(Call<TrainBean> call, Throwable t) {

            }
        });
    }

    //订单
    @Override
    public void signTrain(String trname,String train_id,String telephone,String Anum,String Pay_type,
                          String Aprice,String notice,int userid,final OrderListener listener) {
        trainService = retrofit.create(TrainService.class);
        Call<Integer> call= trainService.signTrain("signtrain",trname,train_id,telephone,Anum,Pay_type,Aprice,notice,userid,"lgj98rfkiqvenhrk2o7oe2po81");
        call.enqueue(new Callback<Integer>() {
            @Override
            public void onResponse(Call<Integer> call, Response<Integer> response) {
                if (response.isSuccessful() && response.body() != null) {
                    listener.onResponse(response.body());
                } else {
                    listener.onFail("onresponse fail");
                }
            }

            @Override
            public void onFailure(Call<Integer> call, Throwable t) {

            }
        });
    }






}
